﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agresso.Interface.Layer.Gateway;
using Agresso.Interface.Layer.Domain;
using System.Data.SqlClient;
using Agresso.Interface.Layer.SQL;
using System.Data;
using System.Collections;

namespace Agresso.Interface.Layer.Service
{
    public class asuheaderService
    {
        DataBaseConnectivity m_dataBaseConectivity;
        public asuheaderService()
        {
            m_dataBaseConectivity = new DataBaseConnectivity();
        }
        public asuheader GetSupplierByName(string name)
        {
            asuheader result = null;
            try
            {
                m_dataBaseConectivity.AgrTempConnectionSQLConnection.Open();
                SqlCommand cmd = new SqlCommand(AgressoSQL.GetSupplierByName(), m_dataBaseConectivity.AgrTempConnectionSQLConnection);
                cmd.Parameters.Add("@apar_name", SqlDbType.NVarChar).Value = name;
                LogService.WriteLogExecuteSQLCommand(cmd);
                SqlDataReader rd = cmd.ExecuteReader();
                while( rd.Read())
                    result = new asuheader(rd);
                rd.Close();
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.AgrTempConnectionSQLConnection.Close();
            }
            return result;
        }
        public asuheader GetSupplierByNameClienS1Like(string name)
        {
            asuheader result = null;
            try
            {
                m_dataBaseConectivity.AgrTempConnectionSQLConnection.Open();
                SqlCommand cmd = new SqlCommand(AgressoSQL.GetSupplierByNameLikeClientS1(name), m_dataBaseConectivity.AgrTempConnectionSQLConnection);
                //cmd.Parameters.Add("@apar_name", SqlDbType.NVarChar).Value = name;
                LogService.WriteLogExecuteSQLCommand(cmd);
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                    result = new asuheader(rd);
                rd.Close();
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.AgrTempConnectionSQLConnection.Close();
            }
            return result;
        }
        public asuheader GetSupplierByCode(string code)
        {
            asuheader result = null;
            try
            {
                m_dataBaseConectivity.AgrTempConnectionSQLConnection.Open();
                SqlCommand cmd = new SqlCommand(AgressoSQL.GetSupplierByCode(), m_dataBaseConectivity.AgrTempConnectionSQLConnection);
                cmd.Parameters.Add("@apar_id", SqlDbType.NVarChar).Value = code;
                LogService.WriteLogExecuteSQLCommand(cmd);
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                    result = new asuheader(rd);
                rd.Close();
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.AgrTempConnectionSQLConnection.Close();
            }
            return result;
        }
        public asuheader GetSupplierByCodeForWinSDS(string code)
        {
            asuheader result = null;
            try
            {
                m_dataBaseConectivity.AgrTempConnectionSQLConnection.Open();
                SqlCommand cmd = new SqlCommand(AgressoSQL.GetSupplierByCodeForWinSDS(), m_dataBaseConectivity.AgrTempConnectionSQLConnection);
                cmd.Parameters.Add("@apar_id", SqlDbType.NVarChar).Value = code;
                LogService.WriteLogExecuteSQLCommand(cmd);
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                    result = new asuheader(rd);
                rd.Close();
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.AgrTempConnectionSQLConnection.Close();
            }
            return result;
        }
        public IList GetListSupplierByName(string name)
        {
            IList result = new ArrayList();
            try
            {
                m_dataBaseConectivity.AgrTempConnectionSQLConnection.Open();
                SqlCommand cmd = new SqlCommand(AgressoSQL.GetSupplierByName(), m_dataBaseConectivity.AgrTempConnectionSQLConnection);
                cmd.Parameters.Add("@apar_name", SqlDbType.NVarChar).Value = name;
                LogService.WriteLogExecuteSQLCommand(cmd);
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    result.Add(new asuheader(rd));
                }
                rd.Close();
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.AgrTempConnectionSQLConnection.Close();
            }
            return result;
        }
        public IList GetSupplierByNameList(string name)
        {
            IList result = new ArrayList();
            try
            {
                m_dataBaseConectivity.AgrTempConnectionSQLConnection.Open();
                SqlCommand cmd = new SqlCommand(AgressoSQL.GetSupplierByNameLike(name), m_dataBaseConectivity.AgrTempConnectionSQLConnection);
                LogService.WriteLogExecuteSQLCommand(cmd);
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    result.Add(new asuheader(rd));
                }
                rd.Close();
            }
            catch (Exception x)
            {
                LogService.WriteLogException(x);
                throw x;
            }
            finally
            {
                m_dataBaseConectivity.AgrTempConnectionSQLConnection.Close();
            }
            return result;
        }

    }
}
